package com.frogs.ucenter.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.util.date.DateUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.UserFamilyStatusEnum;
import com.frogs.ucenter.entity.UserFamilyMember;
import com.frogs.ucenter.service.UserFamilyMemberService;

@Controller
@RequestMapping("/admin/user/familymember")
public class UserFamilyMemberController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userFamilyMember";

    private static final String HOME_VIEW = "admin/userFamilyMember/home";

    private static final String EDIT_VIEW = "admin/userFamilyMember/edit";

    @Autowired
    private UserFamilyMemberService userFamilyMemberService;

    @RequestMapping()
    public String listUserFamilyMember(HttpServletRequest request, ModelMap model) {
        setRequestModelMap( request, model, true );
        model.put("eqStatus", UserFamilyStatusEnum.AVAILABILITY_YES.getId());
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserFamilyMember(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "family",userFamilyMemberService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserFamilyMember(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserFamilyMember userFamilyMember) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
        	userFamilyMember.setCreateTime(DateUtil.getCurrTimestamp());
            userFamilyMemberService.insert( userFamilyMember ); 
            builder = DWZResponse.getSucessBuilder("添加成功");
            builder.navTabId(String.valueOf(model.get("parentRel")));
            builder.callbackType("closeCurrent");
            builder.forwardUrl("/admin/user/familymember?rel=" + String.valueOf(model.get("parentRel")));
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("添加失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserFamilyMember(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model, false);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( userFamilyMemberService.getById( id ) ); 
        }
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserFamilyMember(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserFamilyMember userFamilyMemberNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model, false);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserFamilyMember userFamilyMember = userFamilyMemberService.getById( id ); 
                PropertyUtils.copyProperties(userFamilyMember, userFamilyMemberNew);
                userFamilyMemberService.update( userFamilyMember ); 
                
                builder = DWZResponse.getSucessBuilder("操作成功");
                builder.navTabId(String.valueOf(model.get("parentRel")));
                builder.callbackType("closeCurrent");
                builder.forwardUrl("/admin/user/familymember?rel=" + String.valueOf(model.get("parentRel")));
                model.clear();
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("操作失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserFamilyMember(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids") String inIdList) {
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            model.put("status", UserFamilyStatusEnum.AVAILABILITY_NO.getId());
            userFamilyMemberService.update( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    private PageList<UserFamilyMember> getPageList(ModelMap model) {
        PageList<UserFamilyMember> userFamilyMemberList = userFamilyMemberService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userFamilyMemberList",userFamilyMemberList);
        model.put("pageTurn",userFamilyMemberList.getPageTurn());
        return userFamilyMemberList;
    }
}